package com.ash.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 时间工具
 * 
 * @author Ashin
 *
 */
public class Util {

    private static final long ONE_DAY_MILLIS = 24 * 60 * 60 * 1000L;

    public static long getDelayMilli(String t) throws ParseException {
        SimpleDateFormat simpleDateFormate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat timeDateFormate = new SimpleDateFormat("yyyy-MM-dd");

        String specificTimeStr = timeDateFormate.format(new Date()) + " " + t;
        long specificTimeMillis = simpleDateFormate.parse(specificTimeStr).getTime();
        long currentMills = System.currentTimeMillis();

        long result = currentMills - specificTimeMillis;

        long r = 0L;
        if (result < 0) {
            r = Math.abs(result);

        } else {
            long tmp = specificTimeMillis + ONE_DAY_MILLIS - currentMills;

            r = tmp;
        }

        return r;
    }

    /**
     * 分组一个列表，根据指定的size 把一个list分割成指定大小的list
     * 
     * @param list
     * @param size
     * @return
     */
    public static <T> List<List<T>> splitList(List<T> list, int size) {
        if (list == null) {
            return Collections.emptyList();
        }
        int count = list.size() / size;
        List<List<T>> resultList = new ArrayList<>();
        for (int idx = 0; idx < count; idx++) {
            resultList.add(new ArrayList<>(list.subList(idx * size, (idx + 1) * size)));
        }

        resultList.add(new ArrayList<>(list.subList(count * size, list.size())));

        return resultList;

    }
}
